package cn.fbs.controller.core;

import cn.fbs.service.core.ExtractService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static cn.fbs.util.RequestUtils.getUserName;

@RestController
public class ExtractController {
    @Autowired
    private ExtractService extractService;


    @ApiOperation("根据xml文件提取图像")
    @PostMapping("imageData/extractImage")
    public String extractImageInfo(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String userName = getUserName(request);
        try {
            return extractService.extractImage(file,userName);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().startsWith("400")) {
                return e.getMessage();
            }
            return "500,文件格式不正确";
        }

    }

    @ApiOperation("根据xml文件提取视频")
    @PostMapping("imageData/extractVideo")
    public String extractVideoInfo(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        String userName = getUserName(request);

        try {
            return extractService.extractVideo(file,userName);
        } catch (Exception e) {
            e.printStackTrace();
            return "500,文件格式不正确";
        }
    }
}
